// 简化房间管理系统测试
import SimpleCloudManager from '../network/simpleCloudManager'

class SimpleRoomManagerTest {
  constructor() {
    this.cloudManager = new SimpleCloudManager()
    this.testResults = []
  }

  // 测试房间创建
  async testCreateRoom() {
    console.log('=== 测试房间创建 ===')
    
    try {
      const roomId = await this.cloudManager.createRoom()
      
      if (roomId) {
        console.log('✅ 房间创建成功:', roomId)
        this.testResults.push({
          test: '房间创建',
          status: 'PASS',
          result: roomId
        })
        return roomId
      } else {
        throw new Error('房间创建失败')
      }
    } catch (error) {
      console.error('❌ 房间创建失败:', error)
      this.testResults.push({
        test: '房间创建',
        status: 'FAIL',
        error: error.message
      })
      return null
    }
  }

  // 测试房间加入
  async testJoinRoom(roomId) {
    console.log('=== 测试房间加入 ===')
    
    if (!roomId) {
      console.log('⚠️ 跳过房间加入测试，房间ID无效')
      return false
    }
    
    try {
      const joinedRoomId = await this.cloudManager.joinRoom(roomId)
      
      if (joinedRoomId === roomId) {
        console.log('✅ 房间加入成功:', joinedRoomId)
        this.testResults.push({
          test: '房间加入',
          status: 'PASS',
          result: joinedRoomId
        })
        return true
      } else {
        throw new Error('房间加入失败')
      }
    } catch (error) {
      console.error('❌ 房间加入失败:', error)
      this.testResults.push({
        test: '房间加入',
        status: 'FAIL',
        error: error.message
      })
      return false
    }
  }

  // 测试获取房间信息
  async testGetRoomInfo(roomId) {
    console.log('=== 测试获取房间信息 ===')
    
    if (!roomId) {
      console.log('⚠️ 跳过房间信息测试，房间ID无效')
      return false
    }
    
    try {
      const result = await this.cloudManager.callCloudFunction('get_room_info', {
        roomId: roomId
      })
      
      if (result.success && result.data) {
        console.log('✅ 获取房间信息成功:', result.data)
        this.testResults.push({
          test: '获取房间信息',
          status: 'PASS',
          result: result.data
        })
        return true
      } else {
        throw new Error('获取房间信息失败')
      }
    } catch (error) {
      console.error('❌ 获取房间信息失败:', error)
      this.testResults.push({
        test: '获取房间信息',
        status: 'FAIL',
        error: error.message
      })
      return false
    }
  }

  // 测试下棋功能
  async testMakeMove(roomId) {
    console.log('=== 测试下棋功能 ===')
    
    if (!roomId) {
      console.log('⚠️ 跳过下棋测试，房间ID无效')
      return false
    }
    
    try {
      const result = await this.cloudManager.makeMove(0, 0, 1)
      
      if (result && result.row === 0 && result.col === 0 && result.player === 1) {
        console.log('✅ 下棋成功:', result)
        this.testResults.push({
          test: '下棋功能',
          status: 'PASS',
          result: result
        })
        return true
      } else {
        throw new Error('下棋失败')
      }
    } catch (error) {
      console.error('❌ 下棋失败:', error)
      this.testResults.push({
        test: '下棋功能',
        status: 'FAIL',
        error: error.message
      })
      return false
    }
  }

  // 测试离开房间
  async testLeaveRoom() {
    console.log('=== 测试离开房间 ===')
    
    try {
      await this.cloudManager.leaveRoom()
      console.log('✅ 离开房间成功')
      this.testResults.push({
        test: '离开房间',
        status: 'PASS'
      })
      return true
    } catch (error) {
      console.error('❌ 离开房间失败:', error)
      this.testResults.push({
        test: '离开房间',
        status: 'FAIL',
        error: error.message
      })
      return false
    }
  }

  // 测试OpenID获取
  async testGetOpenId() {
    console.log('=== 测试OpenID获取 ===')
    
    try {
      const openId = await this.cloudManager.getOpenid()
      
      if (openId && openId.length > 0) {
        console.log('✅ OpenID获取成功:', openId)
        this.testResults.push({
          test: 'OpenID获取',
          status: 'PASS',
          result: openId
        })
        return true
      } else {
        throw new Error('OpenID获取失败')
      }
    } catch (error) {
      console.error('❌ OpenID获取失败:', error)
      this.testResults.push({
        test: 'OpenID获取',
        status: 'FAIL',
        error: error.message
      })
      return false
    }
  }

  // 运行所有测试
  async runAllTests() {
    console.log('🚀 开始简化房间管理系统测试...')
    
    // 测试OpenID获取
    await this.testGetOpenId()
    
    // 测试房间创建
    const roomId = await this.testCreateRoom()
    
    // 测试获取房间信息
    await this.testGetRoomInfo(roomId)
    
    // 测试房间加入
    await this.testJoinRoom(roomId)
    
    // 测试下棋功能
    await this.testMakeMove(roomId)
    
    // 测试离开房间
    await this.testLeaveRoom()
    
    // 输出测试结果
    this.printTestResults()
  }

  // 打印测试结果
  printTestResults() {
    console.log('\n📊 测试结果汇总:')
    console.log('=' * 50)
    
    let passCount = 0
    let failCount = 0
    
    this.testResults.forEach((result, index) => {
      const status = result.status === 'PASS' ? '✅' : '❌'
      console.log(`${index + 1}. ${status} ${result.test}`)
      
      if (result.status === 'PASS') {
        passCount++
      } else {
        failCount++
        if (result.error) {
          console.log(`   错误: ${result.error}`)
        }
      }
    })
    
    console.log('=' * 50)
    console.log(`总计: ${this.testResults.length} 个测试`)
    console.log(`通过: ${passCount} 个`)
    console.log(`失败: ${failCount} 个`)
    
    if (failCount === 0) {
      console.log('🎉 所有测试通过！简化房间管理系统工作正常。')
    } else {
      console.log('⚠️ 部分测试失败，请检查相关功能。')
    }
  }

  // 清理资源
  cleanup() {
    if (this.cloudManager) {
      this.cloudManager.cleanup()
    }
  }
}

// 导出测试类
export default SimpleRoomManagerTest 